import {
  IChannel,
  ICheckShareInfo,
  IConfirmLog,
  IFileCheckResponse,
  IFileShareInfo,
  IListing,
  IMember,
  IModifyTaskMetaResponse,
  IOutNumberResponse,
  IPartner,
  IResult,
  IRole,
  ISpace,
  ISpaceInvitation,
  ISubTaskResponse,
  ITaskAttachment,
  ITaskComment,
  ITaskDetailResponse,
  ITaskPhase,
  ITaskStatusBody,
  IUploadResponse,
  IUserInfo,
} from "../interfaces";
import { instance } from "../utils";

export * from "./index-tab";

/** 获取用户信息 */
export const fetchUserInfo = (): Promise<IResult<IUserInfo>> => {
  return instance.get(`/user/token`).then(({ data }) => data);
};

/** 获取空间邀请信息 */
export const fetchinvitation = (
  sign: string,
  referenceId: string = ""
): Promise<IResult<ISpaceInvitation>> => {
  return instance
    .get(`/project/space/invitation?sign=${sign}&referenceId=${referenceId}`)
    .then(({ data }) => data);
};

/** 获取合作方信息 */
export const fetchPartner = (spaceId: string): Promise<IResult<IPartner>> => {
  return instance.get(`/space/${spaceId}/partner`).then(({ data }) => data);
};

/** 获取成员信息 */
export const fetchMember = (spaceId: string): Promise<IResult<IMember[]>> => {
  return instance.get(`/space/${spaceId}/member`).then(({ data }) => data);
};

/** 获取成员权限 */
export const fetchRole = (spaceId: string): Promise<IResult<IRole[]>> => {
  return instance.get(`/space/${spaceId}/role`).then(({ data }) => data);
};
/** 获取频道列表
 * visible不传：全部频道  out：对外可见(H5固定传out)
type筛选类型 file：文件 demand：场景
 */
export const fetchChannel = (
  spaceId: string,
  {
    visible = "out",
    type = "",
  }: { visible?: string; type?: "file" | "demand" | "dashboard" | "" } = {}
): Promise<IResult<IChannel[]>> => {
  return instance
    .get(`/space/${spaceId}/channel?visible=${visible}&type=${type}`)
    .then(({ data }) => data);
};

/**获取空间详情（只能查询空间详情） */
export const fetchSpace = (spaceId: string): Promise<IResult<ISpace>> => {
  return instance.get(`/space/${spaceId}`).then(({ data }) => data);
};

/**生成短连接 */
export const fetchShortUrl = (url: string): Promise<IResult<string>> => {
  return instance.post(`/extra/shortUrl`, { url }).then(({ data }) => data);
};

/**外部成员加入空间 */
export const fetchOuterMember = (
  spaceId: string,
  body: { password: string; referenceId: string }
): Promise<IResult<IOutNumberResponse>> => {
  return instance
    .post(`/space/${spaceId}/member/outer`, body)
    .then(({ data }) => data);
};

/** 获取文件上传地址 */
export const getFileUploadUrl = (
  name: string
): Promise<IResult<IUploadResponse>> => {
  return instance
    .post("/oss/pub/sign/uploadPublicUrl", {
      name,
    })
    .then(({ data }) => data);
};
/** 修改用户信息 */
export const updateUser = ({
  img,
  nickName,
}: {
  img: { bucketName: string; path: string };
  nickName: string;
}) => {
  return instance
    .put("/user", {
      img,
      nickName,
    })
    .then(({ data }) => data);
};

// /** 获取合作方信息 */
// export const fetchTaskDetail = (
//   spaceId: string,
//   listingId: string,
//   confirmId: string
// ): Promise<IResult<IPartner>> => {
//   return instance
//     .get(`/space/${spaceId}/listing/${listingId}/confirm/${confirmId}`)
//     .then(({ data }) => data);
// };

/** 获取合作方信息 */
export const fetchConfirmLog = (
  spaceId: string,
  listingId: string,
  confirmId: string
): Promise<IResult<IConfirmLog[]>> => {
  return instance
    .get(`/space/${spaceId}/listing/${listingId}/confirm/${confirmId}/log`)
    .then(({ data }) => data);
};

/** 获取文件分享信息 */
export const fetchFileShareInfo = (
  shareId: string
): Promise<IResult<IFileShareInfo>> => {
  return instance
    .get(`/space/channel/file/share/${shareId}`)
    .then(({ data }) => data);
};

/** 校验文件分享 */
export const fetchCheckFileShareInfo = (
  shareId: string,
  body: { password?: string }
): Promise<IResult<IFileCheckResponse>> => {
  return instance
    .post(`/space/channel/file/share/${shareId}`, body)
    .then(({ data }) => data);
};

/** 获取确认分享信息 */
export const fetchCheckShareInfo = (
  spaceId: string,
  listingId: string,
  shareId: string
): Promise<IResult<ICheckShareInfo>> => {
  return instance
    .get(`/space/${spaceId}/listing/${listingId}/share/${shareId}`)
    .then(({ data }) => data);
};

/** 获取确认项详情 */
export const fetchProjectTaskDetail = (
  spaceId: string,
  listingId: string,
  confirmId: string
): Promise<IResult<ITaskDetailResponse>> => {
  return instance
    .get(`/space/${spaceId}/listing/${listingId}/confirm/${confirmId}`)
    .then(({ data }) => data);
};

/** 获取任务详情 */
export const fetchTaskDetail = (
  spaceId: string,
  channelId: string,
  viewId: string,
  recordId: string
): Promise<IResult<ITaskDetailResponse>> => {
  return instance
    .get(
      `/space/${spaceId}/channel/${channelId}/view/${viewId}/records/${recordId}`
    )
    .then(({ data }) => data);
};

/** 获取单元格附件 */
export const fetchTaskAttachment = (
  spaceId: string,
  channelId: string,
  viewId: string,
  recordId: string,
  fieldId: string
): Promise<IResult<ITaskAttachment[]>> => {
  return instance
    .get(
      `/space/${spaceId}/channel/${channelId}/view/${viewId}/records/${recordId}/${fieldId}`
    )
    .then(({ data }) => data);
};

/** 获取 子任务 */
export const fetchSubTask = (
  spaceId: string,
  channelId: string,
  viewId: string,
  recordId: string
): Promise<IResult<ISubTaskResponse>> => {
  return instance
    .get(
      `/space/${spaceId}/channel/${channelId}/view/${viewId}/records/${recordId}/childs`
    )
    .then(({ data }) => data);
};

/** 获取阶段列表 */
export const fetchPhaseList = (
  spaceId: string,
  channelId: string
): Promise<IResult<ITaskPhase[]>> => {
  return instance
    .get(`/space/${spaceId}/channel/${channelId}/phase/list`)
    .then(({ data }) => data);
};

/** 任务详情获取评论 */
export const fetchTaskComment = (
  spaceId: string,
  channelId: string,
  opinionId: string
): Promise<IResult<ITaskComment[]>> => {
  return instance
    .get(
      `/space/${spaceId}/channel/${channelId}/comment/list/${opinionId}?externalVisible=true`
    )
    .then(({ data }) => data);
};

/** 修改任务阶段 */
export const modifyTaskPhase = (
  spaceId: string,
  channelId: string,
  viewId: string,
  recordId: string,
  body: {
    oldPhaseId: string;
    phaseId: string;
  }
): Promise<IResult<any>> => {
  return instance
    .put(
      `/space/${spaceId}/channel/${channelId}/view/${viewId}/records/phase/${recordId}`,
      body
    )
    .then(({ data }) => data);
};
/** 修改任务 数据字段 */
export const modifyTaskMeta = (
  spaceId: string,
  channelId: string,
  viewId: string,
  body: {
    recordId: string;
    fieldId: string;
    fieldType: string;
    value: string;
    values?: string[];
    hyperlink?: {
      link: string;
      linkValue: string;
    };
  }
): Promise<IResult<IModifyTaskMetaResponse[]>> => {
  return instance
    .put(`/space/${spaceId}/channel/${channelId}/view/${viewId}/meta`, body)
    .then(({ data }) => data);
};

/** 校验确认分享 */
export const CheckCheckShareInfo = (
  spaceId: string,
  listingId: string,
  shareId: string,
  body: { password?: string }
): Promise<IResult<IFileCheckResponse>> => {
  return instance
    .post(
      `/space/${spaceId}/listing/${listingId}/share/${shareId}/checkPassword`,
      body
    )
    .then(({ data }) => data);
};

/** 确认或驳回 */
export const confirmCheck = (
  spaceId: string,
  listingId: string,
  confirmId: string,
  accessKey: string,
  body: { confirmState: 1 | 2; content: string }
): Promise<IResult<IFileCheckResponse>> => {
  return instance
    .post(
      `/space/${spaceId}/listing/${listingId}/confirm/${confirmId}?accessKey=${accessKey}`,
      body
    )
    .then(({ data }) => data);
};

/** 获取任务的确认记录 */
export const fetchTaskRecord = (
  spaceId: string,
  originId: string
): Promise<IResult<IListing[]>> => {
  return instance
    .get(`/space/${spaceId}/listing/origin/${originId}`)
    .then(({ data }) => data);
};

/** 判断是否已注册 */
export const fetchIsRegister = (
  phoneNumber: string
): Promise<IResult<{ isExist: boolean }>> => {
  return instance
    .get(`/user/phoneNumber/${phoneNumber}`)
    .then(({ data }) => data);
};

/** 判断验证码 */
export const fetchVerification = (phoneNumber: string, randomCode: string) => {
  return instance
    .post(`/user/verification`, {
      msgCode: "register",
      phoneNumber,
      randomCode,
    })
    .then(({ data }) => data);
};
